# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required

import urllib
from .saltapi import salt
from .models import Minion, MinionDetail
from saltops.forms import PaginationForm

def query(req):
    form = PaginationForm(req.GET)
    if not form.is_valid():
        pass
    params = form.getPaginationParams()
    limit   = params['limit']
    offset  = params['offset']
    sort    = params['sort']
    order   = params['order']
    ## 只查看已接受的minion节点(其他类型的节点没有有用信息)
    minions = MinionDetail.objects.all()
    result = {'rows': [], 'total': minions.count()}
    minions = minions[offset: offset + limit]
    result['rows'] = [minion.parse() for minion in minions]
    return JsonResponse(result)

def queryAccepted(req):
    form = PaginationForm(req.GET)
    if not form.is_valid():
        pass
    params = form.getPaginationParams()
    limit   = params['limit']
    offset  = params['offset']
    sort    = params['sort']
    order   = params['order']
    ## 只查看已接受的minion节点(其他类型的节点没有有用信息)
    minions = Minion.objects.filter(type = 0)
    result = {'rows': [], 'total': minions.count()}
    minions = minions[offset: offset + limit]
    result['rows'] = [minion.parse() for minion in minions]
    return JsonResponse(result)

def queryPre(req):
    form = PaginationForm(req.GET)
    if not form.is_valid():
        pass
    params = form.getPaginationParams()
    limit   = params['limit']
    offset  = params['offset']
    sort    = params['sort']
    order   = params['order']
    ## 只查看已接受的minion节点(其他类型的节点没有有用信息)
    minions = Minion.objects.filter(type = 1)
    result = {'rows': [], 'total': minions.count()}
    minions = minions[offset: offset + limit]
    result['rows'] = [minion.parse() for minion in minions]
    return JsonResponse(result)

def queryDenied(req):
    form = PaginationForm(req.GET)
    if not form.is_valid():
        pass
    params = form.getPaginationParams()
    limit   = params['limit']
    offset  = params['offset']
    sort    = params['sort']
    order   = params['order']
    ## 只查看已接受的minion节点(其他类型的节点没有有用信息)
    minions = Minion.objects.filter(type = 2)
    result = {'rows': [], 'total': minions.count()}
    minions = minions[offset: offset + limit]
    result['rows'] = [minion.parse() for minion in minions]
    return JsonResponse(result)

def queryRejected(req):
    form = PaginationForm(req.GET)
    if not form.is_valid():
        pass
    params = form.getPaginationParams()
    limit   = params['limit']
    offset  = params['offset']
    sort    = params['sort']
    order   = params['order']
    ## 只查看已接受的minion节点(其他类型的节点没有有用信息)
    minions = Minion.objects.filter(type = 3)
    result = {'rows': [], 'total': minions.count()}
    minions = minions[offset: offset + limit]
    result['rows'] = [minion.parse() for minion in minions]
    return JsonResponse(result)

def keyOperation(action, match):
    '''
    @match参数格式: 可用通配符, 多个minion id用逗号隔开
    '''
    action = 'list_all' if action == 'query' else action
    params = {
        'client': 'wheel',
        'fun': 'key.' + action,
        'match': match if match else ''
    }
    return salt.postRequest(params)

def delete(req):
    result = {
        'code': 1,
        'msg': 'success'
    }
    id = req.POST.get('id', None)
    try:
        ## 同时从saltstack和saltops数据库中删除
        minion = Minion.objects.filter(id = id).first()
        _kResult = keyOperation('delete', minion.key)
        print(_kResult)
        minion.delete()
    except Exception as e:
        print(e)
        result['code'] = -1
        result['msg'] = e.message
    return JsonResponse(result)
